package com.yanqu.road.server.logic.activity.condition.share;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.ShareInvitationData;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.share.ShareMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ShareEarnSpeedActivityCondition extends BaseActivityCondition {
    public ShareEarnSpeedActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        List<ShareInvitationData> dataList = ShareMgr.getShareInvitationDataList(player.getUserId());
        int rewardTimes = getRewardTimes(getParam());
        String oldParam = getParam();
        BigInteger needEarnSpeed = activityConditionInfo.getParamList().get(3);
        int num = 0;
        BigInteger showEarnSpeed = BigInteger.ZERO;
        for (ShareInvitationData data : dataList) {
            BigInteger earnSpeed = data.getEarnSpeed();
            if (earnSpeed.compareTo(needEarnSpeed) > 0) {
                num++;
                if (num > rewardTimes && earnSpeed.compareTo(showEarnSpeed) > 0) {
                    showEarnSpeed = earnSpeed;
                }
            } else if (earnSpeed.compareTo(showEarnSpeed) > 0) {
                showEarnSpeed = earnSpeed;
            }
        }
        String param = paramToString(rewardTimes, showEarnSpeed, dataList);
        if (!Objects.equals(oldParam, param)) {
            setParam(param);
            setValue(getValue());
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ShareEarnSpeed.getValue(), handle);

    }

    @Override
    public void removeTrigger(GamePlayer player) {
        int rewardTimes = getRewardTimes(getParam());
        int maxTimes = activityConditionInfo.getParamList().get(2).intValue();
        if (rewardTimes >= maxTimes) {
            player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ShareEarnSpeed.getValue(), handle);
        }
    }

    private int getRewardTimes(String str) {
        if (StringUtils.isNullOrEmpty(str)) {
            return 0;
        }
        String[] arr = str.split("\\|");
        return Integer.parseInt(arr[0]);
    }

    private String paramToString(int rewardTimes, BigInteger earnSpeed, List<ShareInvitationData> dataList) {
        List<String> list = new ArrayList<>();
        list.add(String.valueOf(rewardTimes));
        list.add(earnSpeed.toString());
        String invitationStr = "";
        BigInteger needEarnSpeed = activityConditionInfo.getParamList().get(3);
        for (ShareInvitationData data : dataList) {
            if (data.getEarnSpeed().compareTo(needEarnSpeed) < 0) {
                continue;
            }
            long invitationServerId = data.getInvitationServerId();
            long invitationUserId = data.getInvitationUserId();
            invitationStr = invitationStr + invitationServerId + "," + invitationUserId + ";";
        }
        if (invitationStr.length() > 0) {
            invitationStr = invitationStr.substring(0, invitationStr.length() - 1);
        }
        list.add(invitationStr);
        return String.join("|", list);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        int rewardTimes = getRewardTimes(getParam());
        int maxTimes = activityConditionInfo.getParamList().get(2).intValue();
        if (rewardTimes >= maxTimes) {
            return false;
        }
        List<ShareInvitationData> dataList = ShareMgr.getShareInvitationDataList(player.getUserId());
        BigInteger needEarnSpeed = activityConditionInfo.getParamList().get(3);
        int num = 0;
        for (ShareInvitationData data : dataList) {
            BigInteger earnSpeed = data.getEarnSpeed();
            if (earnSpeed.compareTo(needEarnSpeed) >= 0) {
                num++;
            }
        }
        return num > rewardTimes;
    }

    @Override
    public void initPreValue() {
        String param = getParam();
        if (StringUtils.isNullOrEmpty(param)) {
            String s = paramToString(0, BigInteger.ZERO, new ArrayList<>());
            setParam(s);
        }
    }

    public String getReward() {
        String reward = "";
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(getConditionId());
        if(!userActivityConditionData.isGetReward()) {
            reward = activityConditionInfo.getRewardStr();
            player.getModule(CurrencyModule.class).addCurrency(activityConditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.ActivityReward);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);


            List<ShareInvitationData> dataList = ShareMgr.getShareInvitationDataList(player.getUserId());
            int rewardTimes = getRewardTimes(getParam()) + 1;
            BigInteger needEarnSpeed = activityConditionInfo.getParamList().get(3);
            int num = 0;
            BigInteger showEarnSpeed = BigInteger.ZERO;
            for (ShareInvitationData data : dataList) {
                BigInteger earnSpeed = data.getEarnSpeed();
                if (earnSpeed.compareTo(needEarnSpeed) > 0) {
                    num++;
                    if (num > rewardTimes && earnSpeed.compareTo(showEarnSpeed) > 0) {
                        showEarnSpeed = earnSpeed;
                    }
                } else if (earnSpeed.compareTo(showEarnSpeed) > 0) {
                    showEarnSpeed = earnSpeed;
                }
            }
            String param = paramToString(rewardTimes, showEarnSpeed, dataList);
            setParam(param);
            setValue(getValue());


            LogMgr.addLogActivityReward(player.getUserId(), activityInfo.getActivityId(), activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), 0, false, activityConditionInfo.getRewardStr());
        }else {
            logger.error("already get reward activityId:{}; conditionId:{}", activityInfo.getActivityId(), getConditionId());
        }
        return reward;
    }


}
